import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by shaxi on 2016/2/17.
 */
public class LockDemo implements Runnable {
    private int counter = 0;

    public void run() {
        int loopTimes = 10000;
        ReentrantLock lock = new ReentrantLock();
        lock.lock();
        while (loopTimes > 0) {
            counter++;
            loopTimes--;
        }
        lock.unlock();
    }

    public static void main(String[] args) throws InterruptedException {
        LockDemo demo = new LockDemo();

        Thread[] threads = new Thread[]{
                new Thread(demo), new Thread(demo), new Thread(demo), new Thread(demo), new Thread(demo)
        };


        for (Thread t : threads) {
            t.start();
        }

        for (Thread t : threads) {
            t.join();
        }

        System.out.println("demo's counter is " + demo.counter);
    }
}
